package com.yanqu.road.server.manger.activity.tanghulu;

import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.tanghulu.TangHuLuMakeData;
import com.yanqu.road.entity.activity.tanghulu.TangHuLuScoreInfo;
import com.yanqu.road.entity.activity.tanghulu.TangHuLuUserData;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.logic.bussiness.activity.TangHuLuBusiness;
import com.yanqu.road.pb.activity.TangHuLuProto;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.tanghulu.TangHuLuModule;
import com.yanqu.road.server.manger.ServerLanguageMgr;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.utils.RandomHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;

public class TangHuLuMgr extends TempMgr {

    public static ActivityInfo activityInfo;

    public static List<TangHuLuScoreInfo>  scoreInfoList;

    static RandomHelper randomHelper = new RandomHelper();

    //糖葫芦积分道具id
    public static int SCORE_GOODS_ID = 101937;

    //山楂道具id
    public static int TANGHULU_HAWTHORN_ID = 120272;
    //杨桃道具id
    public static int TANGHULU_CARAMBOLA_ID = 120273;
    //猕猴桃道具id
    public static int TANGHULU_KIWIFRUIT_ID = 120274;
    //草莓道具id
    public static int TANGHULU_STRAWBERRY_ID = 120275;

    //圆盘等份
    public static int EQUAL_DIVISION = 36;

    //每秒钟转速（角度）
    public static int TURN_ANGLE_PER_SECOND = 360 * 2;

    //闪光点分数
    public static int FLASH_POINT_SCORE = 10;

    //闪光点生成权重
    public static List<Integer> FLASH_POINT_WEIGHT_LIST;

    //可变转速规则
    public static List<TangHuLuProto.TangHuLuPanSpeedConfigTemp> VARIABLE_TURN_SPEED_LIST;

    //高倍场消耗
    public static int SENIOR_CONSUME_MULTIPLE = 4;
    //童心值
    public static int SENIOR_OPEN_VALUE = 10000;

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }


    /**
     * 获取展示中的活动ID（0表示没活动）
     */
    public static int getShowTimeActivityId() {
        return activityInfo == null ? 0 : activityInfo.getActivityId();
    }

    public static boolean activityInTime(){
        return ActivityMgr.activityInTime(activityInfo);
    }

    public static ActivityInfo getActivityInfo() {
        return activityInfo;
    }

    public static void setActivityInfo(ActivityInfo activityInfo) {
        TangHuLuMgr.activityInfo = activityInfo;
    }

    public static void reloadActivityData() {
        getLogger().info("reload tanghulu activity start");
        // 获取活动
        ActivityInfo tmpActivityInfo;
        List<ActivityInfo> openActivityInfoList = NormalActivityMgr.getOpenActivityInfoList(eActivityType.TangHuluActivity.getValue());
        if (openActivityInfoList.size() <= 0) {
            return;
        }
        tmpActivityInfo = openActivityInfoList.get(0);

        List<TangHuLuScoreInfo> tempScoreInfoList = TangHuLuBusiness.getScoreRuleConfig(tmpActivityInfo.getActivityId());

        //积分道具id
        List<Integer> param2List = StringUtils.stringToIntegerList(tmpActivityInfo.getParam2(), "\\|");
        if(param2List.size() > 1){
            SCORE_GOODS_ID = param2List.get(0);
        }

        //水果道具id
        List<Integer> param3List = StringUtils.stringToIntegerList(tmpActivityInfo.getParam3(), "\\|");
        if(param3List.size() == 4){
            TANGHULU_HAWTHORN_ID = param3List.get(0);
            TANGHULU_CARAMBOLA_ID = param3List.get(1);
            TANGHULU_KIWIFRUIT_ID = param3List.get(2);
            TANGHULU_STRAWBERRY_ID = param3List.get(3);
        }

        // 活动配置
        Map<String, ActivityConfig> activityConfigMap = NormalActivityMgr.getActivityConfigMap(tmpActivityInfo.getActivityId());
        if(activityConfigMap != null){
            if(activityConfigMap.containsKey("EQUAL_DIVISION")){
                //圆盘等份
                EQUAL_DIVISION = activityConfigMap.get("EQUAL_DIVISION").getIntValue();
            }
            if(activityConfigMap.containsKey("FLASH_POINT_SCORE")){
                //闪光点积分
                FLASH_POINT_SCORE = activityConfigMap.get("FLASH_POINT_SCORE").getIntValue();
            }
            if(activityConfigMap.containsKey("FLASH_POINT_WEIGHT_LIST")){
                //闪光点权重
                FLASH_POINT_WEIGHT_LIST = StringUtils.stringToIntegerList(activityConfigMap.get("FLASH_POINT_WEIGHT_LIST").getValue(), "\\|");
            }
            if(activityConfigMap.containsKey("VARIABLE_TURN_SPEED_LIST")){
                //可变转速
                List<TangHuLuProto.TangHuLuPanSpeedConfigTemp> tempList = new ArrayList<>();
                List<String> list = StringUtils.stringToStringList(activityConfigMap.get("VARIABLE_TURN_SPEED_LIST").getValue(), "\\|");
                for(String str  : list){
                    List<Integer> valList = StringUtils.stringToIntegerList(str, ",");
                    if(valList.size() == 2){
                        TangHuLuProto.TangHuLuPanSpeedConfigTemp.Builder builder = TangHuLuProto.TangHuLuPanSpeedConfigTemp.newBuilder();
                        builder.setNum(valList.get(0));
                        builder.setTurnSpeed(valList.get(1));
                        tempList.add(builder.build());
                    }
                }
                VARIABLE_TURN_SPEED_LIST = tempList;
            }
            SENIOR_CONSUME_MULTIPLE = activityConfigMap.get("SENIOR_CONSUME_MULTIPLE").getIntValue();
            SENIOR_OPEN_VALUE = activityConfigMap.get("SENIOR_OPEN_VALUE").getIntValue();
        }

        activityInfo = tmpActivityInfo;
        scoreInfoList = tempScoreInfoList;
        reloadPlayerData();
        getLogger().info("reload tanghulu activity end");
    }

    private static void reloadPlayerData() {
        if (ActivityMgr.activityInShowTime(activityInfo)) {
            for (GamePlayer player : GamePlayerMgr.getCopyPlayerMap().values()) {
                if (SystemOpenMgr.systemOpen(player, eSystemId.TangHuluActivity.getValue())) {
                    TangHuLuModule module = player.getModule(TangHuLuModule.class);
                    module.dailyReset();
                    if (module.getUserData() == null || module.getUserData().getActivityId() != activityInfo.getActivityId()) {
                        module.initUserData();
                        module.syncUserData();
                    }
                }
            }
        }
    }

    /**
     * 随机获取闪光点
     * @return
     */
    public static List<Integer> getRandomFlashPoint(){
        List<Integer> pointList = new ArrayList<>();
        int equalDivision = EQUAL_DIVISION;
        int start = randomHelper.next(1, equalDivision + 1);
        Random random = new Random();
        int index = RandomHelper.getRandomIndexByWeight(FLASH_POINT_WEIGHT_LIST, random);
        if(index > 0){
            for(int i = 1; i <= index; i++){
                if(start > equalDivision){
                    start = 1;
                }
                pointList.add(start);
                start++;
            }
        }
        return pointList;
    }

    /**
     * 根据命中个数获取单局评分规则
     * @param num
     * @return
     */
    public static TangHuLuScoreInfo getScoreInfoByNum(int num){
        TangHuLuScoreInfo scoreInfo = null;
        for(TangHuLuScoreInfo info : scoreInfoList){
            if(num >= info.getNumRange().get(0) && num <= info.getNumRange().get(1)){
                scoreInfo = info;
                break;
            }
        }
        return scoreInfo;
    }


    /**
     * 是否在清空道具时间内
     */
    public static boolean isActivityInClearTime() {
        return ActivityMgr.activityInShowPeriod(activityInfo) || ActivityMgr.activityInRewardPeriod(activityInfo);
    }

    public static TangHuLuProto.TangHuLuScoreInfoTemp.Builder getScoreConfigInfoBuilder(TangHuLuScoreInfo info, GamePlayer player){
        TangHuLuProto.TangHuLuScoreInfoTemp.Builder builder = TangHuLuProto.TangHuLuScoreInfoTemp.newBuilder();
        builder.setId(info.getId());
        builder.setDesc(ServerLanguageMgr.getContent(info.getDesc(), player.getUserInfo().getLanguage()));
        builder.setName(ServerLanguageMgr.getContent(info.getName(), player.getUserInfo().getLanguage()));
        //builder.setDesc(info.getDesc());
        //builder.setName(info.getName());
        builder.setIntegral(info.getIntegral());
        return builder;
    }

    public static TangHuLuProto.TangHuLuMakeDataTemp.Builder getMakeDataBuilder(TangHuLuMakeData makeData) {
        TangHuLuProto.TangHuLuMakeDataTemp.Builder makeDataBuilder = TangHuLuProto.TangHuLuMakeDataTemp.newBuilder();
        makeDataBuilder.setBeginTime(makeData.getInTime());
        makeDataBuilder.setScore(makeData.getScore());
        makeDataBuilder.addAllFlashPoint(makeData.getFlashPoint());

        for(Map.Entry<Integer, List<Integer>> sectionEntry : makeData.getData().entrySet()){
            TangHuLuProto.TangHuLuPanDataTemp.Builder builder = TangHuLuProto.TangHuLuPanDataTemp.newBuilder();
            builder.setSection(sectionEntry.getKey());
            builder.addAllGoodsId(sectionEntry.getValue());
            makeDataBuilder.addGameData(builder);
        }
        return makeDataBuilder;
    }

    public static TangHuLuProto.TangHuLuUserDataTemp.Builder getUserDataBuilder(TangHuLuUserData userData) {
        TangHuLuProto.TangHuLuUserDataTemp.Builder dataBuilder = TangHuLuProto.TangHuLuUserDataTemp.newBuilder();
        dataBuilder.setEqualDivision(TangHuLuMgr.EQUAL_DIVISION);
        dataBuilder.setTurnSpeed(TangHuLuMgr.TURN_ANGLE_PER_SECOND);
        dataBuilder.setSweetVal(userData.getSweetVal());
        dataBuilder.setTangHuLuNum(userData.getTangHuLuNum());
        dataBuilder.setTotalScore(userData.getTotalScore());
        dataBuilder.addAllChangeSpeed(VARIABLE_TURN_SPEED_LIST);
        dataBuilder.setSeniorOpenVal(SENIOR_OPEN_VALUE);
        return dataBuilder;
    }

}
